package com.lizard.schedule.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lizard.schedule.R;
import com.lizard.schedule.net.http.request.bean.ScheduleSync.ScheduleSyncItem;
import com.lizard.schedule.ui.activity.base.BaseActivity;
import com.lizard.schedule.ui.adapter.RepeatListAdapter;
import com.lizard.schedule.ui.view.Titlebar;
import com.lizard.schedule.utils.RepeatScheduleUtil;
import com.lizard.schedule.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * "重复时间"设置
 *
 * Created by wangyiqian on 16/1/1.
 */
public class RepeatSettingActivity extends BaseActivity{

    public final static String EXTRA_REPEAT_TYPE = "extra_repeat_type";

    public final static String BACK_EXTRA_REPEAT_TYPE = "back_extra_repeat_type";

    private ListView repeatList;

    private RepeatListAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_repeat);

        // 设置状态栏颜色
        StatusBarUtil.setStatusBarColor(this, R.color.schedule_blue);

        initView();
    }

    private void initView(){
        initTitle();
        repeatList = (ListView) findViewById(R.id.repeat_list);
        repeatList.setSelector(R.color.common_transparent);
        adapter = new RepeatListAdapter(this);
        repeatList.setAdapter(adapter);
        adapter.setData(genInitData());
        repeatList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RepeatListAdapter.ItemContent itemContent = (RepeatListAdapter.ItemContent) (parent.getAdapter()).getItem(position);
                itemContent.selected = !itemContent.selected;
                adapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 标题栏UI初始化
     */
    private void initTitle() {
        Titlebar titlebar = (Titlebar) findViewById(R.id.title_bar);

        titlebar.setLeftBtnClk(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                back();
            }
        });

        titlebar.setRightBtnClk(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                int repeat = ScheduleSyncItem.REPEAT_NONE;
                List<RepeatListAdapter.ItemContent> itemContents = adapter.getData();
                for (RepeatListAdapter.ItemContent itemContent : itemContents) {
                    if (itemContent.selected) {
                        repeat = RepeatScheduleUtil.mergeRepeat(repeat, itemContent.repeat);
                    }
                }
                intent.putExtra(BACK_EXTRA_REPEAT_TYPE, repeat);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    /**
     * 返回初始化数据
     *
     * @return
     */
    private List<RepeatListAdapter.ItemContent> genInitData(){

        int initialRepeatType = getIntent().getIntExtra(EXTRA_REPEAT_TYPE, ScheduleSyncItem.REPEAT_NONE);

        List<RepeatListAdapter.ItemContent> itemContents = new ArrayList<RepeatListAdapter.ItemContent>();

        RepeatListAdapter.ItemContent itemSunday = new RepeatListAdapter.ItemContent();
        itemSunday.repeat = ScheduleSyncItem.REPEAT_SUNDAY;
        itemSunday.title = getString(R.string.repeat_sunday);
        itemSunday.selected = RepeatScheduleUtil.containRepeat(initialRepeatType, itemSunday.repeat);
        itemContents.add(itemSunday);

        RepeatListAdapter.ItemContent itemMonday = new RepeatListAdapter.ItemContent();
        itemMonday.repeat = ScheduleSyncItem.REPEAT_MONDAY;
        itemMonday.title = getString(R.string.repeat_monday);
        itemMonday.selected = RepeatScheduleUtil.containRepeat(initialRepeatType, itemMonday.repeat);
        itemContents.add(itemMonday);

        RepeatListAdapter.ItemContent itemTuesday = new RepeatListAdapter.ItemContent();
        itemTuesday.repeat = ScheduleSyncItem.REPEAT_TUESDAY;
        itemTuesday.title = getString(R.string.repeat_tuesday);
        itemTuesday.selected = RepeatScheduleUtil.containRepeat(initialRepeatType, itemTuesday.repeat);
        itemContents.add(itemTuesday);

        RepeatListAdapter.ItemContent itemWednesday = new RepeatListAdapter.ItemContent();
        itemWednesday.repeat = ScheduleSyncItem.REPEAT_WEDNESDAY;
        itemWednesday.title = getString(R.string.repeat_wednesday);
        itemWednesday.selected = RepeatScheduleUtil.containRepeat(initialRepeatType, itemWednesday.repeat);
        itemContents.add(itemWednesday);

        RepeatListAdapter.ItemContent itemThursday = new RepeatListAdapter.ItemContent();
        itemThursday.repeat = ScheduleSyncItem.REPEAT_THURSDAY;
        itemThursday.title = getString(R.string.repeat_thursday);
        itemThursday.selected = RepeatScheduleUtil.containRepeat(initialRepeatType, itemThursday.repeat);
        itemContents.add(itemThursday);

        RepeatListAdapter.ItemContent itemFriday = new RepeatListAdapter.ItemContent();
        itemFriday.repeat = ScheduleSyncItem.REPEAT_FRIDAY;
        itemFriday.title = getString(R.string.repeat_friday);
        itemFriday.selected = RepeatScheduleUtil.containRepeat(initialRepeatType, itemFriday.repeat);
        itemContents.add(itemFriday);

        RepeatListAdapter.ItemContent itemSaturday = new RepeatListAdapter.ItemContent();
        itemSaturday.repeat = ScheduleSyncItem.REPEAT_SATURDAY;
        itemSaturday.title = getString(R.string.repeat_saturday);
        itemSaturday.selected = RepeatScheduleUtil.containRepeat(initialRepeatType, itemSaturday.repeat);
        itemContents.add(itemSaturday);

        return itemContents;
    }

    private void back(){
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        back();
    }
}
